/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ScannerParser;

/**
 *
 * @author jefoutta
 */
public class Variable extends Symbol{

    public Variable(String tlexema, String ttype, String tsize){
        //System.out.println("se creo una variable------------------\nlexema: "+tlexema+"\ntipo: "+ttype+"\ntamaño: "+tsize);
        this.name = tlexema;
        this.type = ttype;
        this.size = Integer.parseInt(tsize);
        this.typeSymbol = "variable";
        if(!(ttype.equals("int")||ttype.equals("char")||ttype.equals("void")||ttype.equals("boolean"))){
            SymbolTable temp = SymbolTable.actualSymbolTable.getChildRecursive("struct "+this.type);
            if(temp != null){
                SymbolTable.actualSymbolTable.addChildSymbolTable(0,this.typeSymbol+"_"+this.name);
                /*for(int i = 0; i < temp.getSymbolList().size(); i++){
                    SymbolTable.actualSymbolTable.addSymbol(new Variable(temp.getSymbolList().get(i).name,temp.getSymbolList().get(i).type,""+temp.getSymbolList().get(i).size));
                }*/
                this.width = temp.getWidth();
                this.position = SymbolTable.actualSymbolTable.getWidth();
                this.base = SymbolTable.actualSymbolTable.name;
                SymbolTable.actualSymbolTable.addWidth(this.width);
                this.totalWidth = this.width * this.size;
                //SymbolTable.actualSymbolTable.returnToParent();
            }else{
                SymbolTable.actualSymbolTable.addChildSymbolTable(0,this.typeSymbol+"_"+this.name);
                //SymbolTable.actualSymbolTable.returnToParent();
                SymbolTable.pending.add(this);
                SymbolTable.pendingChild.add(SymbolTable.actualSymbolTable.getLastChild());
            }
        }else{
            if(ttype.equals("int"))
                this.width = 2;
            else
                if(ttype.equals("char"))
                    this.width = 2;
                else
                    if(ttype.equals("boolean"))
                        this.width = 2;
            this.position = SymbolTable.actualSymbolTable.getWidth();
            this.totalWidth = this.width * this.size;
            this.base = SymbolTable.actualSymbolTable.getName();
            SymbolTable.actualSymbolTable.addChildSymbolTable(0,this.typeSymbol+"_"+this.name);
            SymbolTable.actualSymbolTable.addWidth(this.totalWidth);
        }
    }

    public Variable(String tlexema, String ttype, String tsize,boolean isParam){
        //System.out.println("se creo una variable------------------\nlexema: "+tlexema+"\ntipo: "+ttype+"\ntamaño: "+tsize);
        this.name = tlexema;
        this.type = ttype;
        this.size = Integer.parseInt(tsize);
        this.typeSymbol = "variable";
        if(!(ttype.equals("int")||ttype.equals("char")||ttype.equals("void")||ttype.equals("boolean"))){
            SymbolTable temp = SymbolTable.actualSymbolTable.getChildRecursive("struct "+this.type);
            if(temp != null){
                SymbolTable.actualSymbolTable.addChildSymbolTable(0,this.typeSymbol+"_"+this.name);
                /*for(int i = 0; i < temp.getSymbolList().size(); i++){
                    SymbolTable.actualSymbolTable.addSymbol(new Variable(temp.getSymbolList().get(i).name,temp.getSymbolList().get(i).type,""+temp.getSymbolList().get(i).size));
                }*/
                this.width = temp.getWidth();
                this.position = SymbolTable.actualSymbolTable.getWidth();
                this.base = SymbolTable.actualSymbolTable.name;
                SymbolTable.actualSymbolTable.addWidth(this.width);
                this.totalWidth = this.width * this.size;
                //SymbolTable.actualSymbolTable.returnToParent();
            }else{
                SymbolTable.actualSymbolTable.addChildSymbolTable(0,this.typeSymbol+"_"+this.name);
                //SymbolTable.actualSymbolTable.returnToParent();
                SymbolTable.pending.add(this);
                SymbolTable.pendingChild.add(SymbolTable.actualSymbolTable.getLastChild());
            }
        }else{
            if(ttype.equals("int"))
                this.width = 2;
            else
                if(ttype.equals("char"))
                    this.width = 2;
                else
                    if(ttype.equals("boolean"))
                        this.width = 2;
            this.position = SymbolTable.actualSymbolTable.getWidth();
            this.totalWidth = this.width * this.size;
            this.base = SymbolTable.actualSymbolTable.getName();
            SymbolTable.actualSymbolTable.addChildSymbolTable(0,this.typeSymbol+"_"+this.name);
            SymbolTable.actualSymbolTable.addWidth(this.totalWidth);
            SymbolTable.actualSymbolTable.addParamWidth(this.totalWidth);
        }
    }

    public Variable(String tlexema){
        this.name = tlexema;
    }

    public Variable(String tlexema, String ttype){
        this.name = tlexema;
        this.type = ttype;
    }

    public String toString(){
        return "Variable "+ this.name + " " +this.type+" P"+this.position;
    }
}
